package com.llkj.qingnianguoxue.music;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.MusicLikeAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.CourseEvaluateBean;
import com.llkj.qingnianguoxue.bean.MusicHomeBean;
import com.llkj.qingnianguoxue.bean.MusicLikeBean;
import com.llkj.qingnianguoxue.bean.response.ResponseList;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MusicLikeActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener{
    @BindView(R.id.refresh_layout)
    protected SwipeRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    protected RecyclerView lvMusicView;//
    private MusicLikeAdapter mMusicLikeAdapter;
    private List<MusicLikeBean> mMusicData;
    private int totalSize,mPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_like);
        ButterKnife.bind(this);
        setTitle("我喜欢", true, TITLE_TYPE_IMG, R.drawable.icon_back, false,-1,-1);

        initView();
        initListener();

        loadWaitDialog();
        onRefresh();
    }
    private void initView(){
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvMusicView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false));

        mMusicData = new ArrayList<>();
        mMusicLikeAdapter = new MusicLikeAdapter(R.layout.item_music_like,mMusicData);
        lvMusicView.setAdapter(mMusicLikeAdapter);
    }

    private void initListener(){
        refreshLayout.setOnRefreshListener(this);
        mMusicLikeAdapter.setOnLoadMoreListener(this);
        lvMusicView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                MusicLikeBean item = mMusicData.get(i);
                Bundle bundle  = new Bundle();
                bundle.putString("musicId",item.getKoji_id());
                startActivity(MusicDetailActivity.class,bundle);
            }
        });
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getData();
    }
    @Override
    public void onLoadMore() {
        mPage++;
        getData();
    }

    private void getData(){
        HttpMethod.kojiLike(this,this,String.valueOf(mPage));
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, mMusicLikeAdapter, null, lvMusicView, totalSize);
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
//        refreshLayout.setRefreshing(false);
        if (HttpMethod.HTTP_KOJI_LIKE == method) {//2.1首页
            ResponseList<MusicLikeBean> response = GsonUtil.fromJsonList(data,MusicLikeBean.class);
            if(response!=null){
                totalSize = Integer.parseInt(response.getNum());
                if (mPage == 1) {
                    mMusicData.clear();
                    LoadDataHelper.refreshComplete(mMusicLikeAdapter, mMusicData, refreshLayout, lvMusicView);
                } else {
                    mMusicLikeAdapter.setEnableLoadMore(true);
                    mMusicLikeAdapter.loadMoreComplete();
                }
                mMusicData.addAll(response.getLike());
                mMusicLikeAdapter.notifyDataSetChanged();
            }
        }
    }
}
